package  {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.objects.primitives.Cube;
	import CubeMaker;
	import DisplayerTwo;
	import ImageSplitter;
	import SplitEvent;
	import CubeEvent;
	import BannerDisplayer;
	import CubeXSetter;
	
	public class DocumentClass extends MovieClip {
		
		private var _stageHeight:Number;
		private var _stageWidth:Number;
		private var _imgArray:Array;
		private var _bannerArray:Array = [];
		private var _onSplitCounter:int = 0;
		private var _splitCounter:int = 0;
		private var _numberOfBanners:int = 3 //use this to select how many banners will be displayed
		private var _numberOfPieces:int = 10; //use this to select how many image parts you want
		private var _hSpace:int = 5; //use this to set the distance between pieces. _evenHorizontalSpace must be true to use this
		private var _evenHorizontalSpace:Boolean = false; //set this to true to evenly space the pieces
		private var _cubeMaker:CubeMaker;
		private var _renderer:BasicRenderEngine = new BasicRenderEngine();
		private var _viewport:Viewport3D = new Viewport3D(0,0,true,true);
		private var _camera:Camera3D = new Camera3D();
		private var _scene:Scene3D = new Scene3D();
		private var _myCube:Cube;
		private var _cubeArray:Array = [];
		internal static var _base;
		
		public function DocumentClass() {
			DocumentClass._base = this;
			tf_pieceInput.text = _numberOfPieces.toString();
			reset_mc.visible = false;
			reset_mc.addEventListener(MouseEvent.CLICK, onReset);
			button_mc.buttonMode = true;
			button_mc.addEventListener(MouseEvent.CLICK, onClick);
			//button_mc.dispatchEvent(new Event(MouseEvent.CLICK));	
		}
		
		private function onReset(e:Event):void {
			button_mc.visible = false;
			textBoxes_mc.visible = false;
			tf_title.visible = false;
			tf_imageNumber.visible = false;
			tf_pieceNumber.visible = false;
			tf_space.visible = false;
			tf_gap.visible = false;
			tf_imageNumberInput.visible = false;
			tf_pieceInput.visible = false;
			tf_spaceBoolInput.visible = false;
			tf_gapInput.visible = false;
			reset_mc.visible = false;
		}
		
		private function onClick(e:Event):void {
			button_mc.visible = false;
			textBoxes_mc.visible = false;
			tf_title.visible = false;
			tf_imageNumber.visible = false;
			tf_pieceNumber.visible = false;
			tf_space.visible = false;
			tf_gap.visible = false;
			tf_imageNumberInput.visible = false;
			tf_pieceInput.visible = false;
			tf_spaceBoolInput.visible = false;
			tf_gapInput.visible = false;
			//reset_mc.visible = true;
			trace(tf_gapInput.text);
			_numberOfBanners = int(tf_imageNumberInput.text);
			_numberOfPieces = int(tf_pieceInput.text);
			if (tf_spaceBoolInput.text == "false") {
				_evenHorizontalSpace = false;
			} else if (tf_spaceBoolInput.text == "true") {
				_evenHorizontalSpace = true;
			}
			_hSpace = int(tf_gapInput.text);
			
			
			//for number of images... i < numberOfImages
			_stageHeight = stage.stageHeight;
			_stageWidth = stage.stageWidth;
			_imgArray = ["banners/banner1.jpg", "banners/banner2.jpg", "banners/banner3.jpg", "banners/banner4.jpg"];
			_imgArray.splice(_numberOfBanners, (_imgArray.length - _numberOfBanners));
			//, "banners/banner3.jpg", "banners/banner4.jpg"
			for (var i = 0	; i < _imgArray.length; i++) {
				split(_imgArray[i],i);
			}
		}
		
		private function split(imgPath:String,position:int) {
			var splitter:ImageSplitter;
			var imageParts:Array;
			splitter = new ImageSplitter(imgPath,position,_numberOfPieces);
			splitter.addEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
			function onSplit(e:SplitEvent):void {
				trace(e);
				var tempPosition:int = int(e.position);
				imageParts = splitter.imageParts;
				afterSplit(imageParts, tempPosition, _splitCounter);
				_splitCounter++;
			}
		}
		
		private function afterSplit(imgParts, position:int, count) {
			_bannerArray[position] = imgParts;
			if ((count + 1) == _imgArray.length) arrayFull();
		}
		
		private function arrayFull() {
			var temp:Array = _bannerArray[1];
			//addChild(temp[0]);
			//var displayerTwo:DisplayerTwo = new DisplayerTwo(_bannerArray, _stageHeight, _stageWidth, _evenHorizontalSpace, _hSpace);
			
			_cubeMaker = new CubeMaker(_bannerArray, _numberOfPieces);
			_cubeMaker.addEventListener(CubeEvent.CUBES_MADE, onCubesMade);
			_cubeMaker.makeCubes();
		}
		
		private function onCubesMade(e:CubeEvent):void {
			trace(e);
			_cubeArray = _cubeMaker.cubes;
			var xSetter:CubeXSetter = new CubeXSetter(_cubeMaker.cubeWidth, _cubeArray.length);
			xSetter.addEventListener(Event.COMPLETE, onXSetterMade);
			var bannerDisplayer:BannerDisplayer = new BannerDisplayer(_cubeArray,_cubeMaker.cubeWidth);
			trace("cubeArray filled. This many pieces: " + _cubeArray.length);
			addChild(_viewport);
			_viewport.buttonMode = true;
									
			_camera.zoom = 10;
			_camera.focus = 100; 
			
			for (var i = 0; i < _cubeArray.length; i++) {
				_cubeArray[i].x = xSetter.cubeX(i);
				_cubeArray[i].rotationX -= xSetter.rotationX(i);
				_scene.addChild(_cubeArray[i]);
			}
			addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function onXSetterMade(e:Event):void {
			trace("complete called");
		}
		
		private function loop(e:Event):void{
			//_myCube.rotationY += 3;
			for (var i = 0; i < _cubeArray.length; i++) {
				_cubeArray[i].rotationX += 3;
			}
			_renderer.renderScene(_scene, _camera, _viewport);
		}
	}	
}